
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>泛型参数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="10_Summary_of_the_Grammar.html" />
    
    
    <link rel="prev" href="08_Patterns.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/01_The_Basics.html">
            
                <a href="../chapter2/01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/02_Basic_Operators.html">
            
                <a href="../chapter2/02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/03_Strings_and_Characters.html">
            
                <a href="../chapter2/03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/04_Collection_Types.html">
            
                <a href="../chapter2/04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/05_Control_Flow.html">
            
                <a href="../chapter2/05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../chapter2/06_Functions.html">
            
                <a href="../chapter2/06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../chapter2/07_Closures.html">
            
                <a href="../chapter2/07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../chapter2/08_Enumerations.html">
            
                <a href="../chapter2/08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../chapter2/09_Structures_And_Classes.html">
            
                <a href="../chapter2/09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../chapter2/10_Properties.html">
            
                <a href="../chapter2/10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../chapter2/11_Methods.html">
            
                <a href="../chapter2/11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../chapter2/12_Subscripts.html">
            
                <a href="../chapter2/12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../chapter2/13_Inheritance.html">
            
                <a href="../chapter2/13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../chapter2/14_Initialization.html">
            
                <a href="../chapter2/14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../chapter2/15_Deinitialization.html">
            
                <a href="../chapter2/15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../chapter2/16_Optional_Chaining.html">
            
                <a href="../chapter2/16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../chapter2/17_Error_Handling.html">
            
                <a href="../chapter2/17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../chapter2/18_Type_Casting.html">
            
                <a href="../chapter2/18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../chapter2/19_Nested_Types.html">
            
                <a href="../chapter2/19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../chapter2/20_Extensions.html">
            
                <a href="../chapter2/20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../chapter2/21_Protocols.html">
            
                <a href="../chapter2/21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../chapter2/22_Generics.html">
            
                <a href="../chapter2/22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../chapter2/23_Automatic_Reference_Counting.html">
            
                <a href="../chapter2/23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../chapter2/24_Memory_Safety.html">
            
                <a href="../chapter2/24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="../chapter2/25_Access_Control.html">
            
                <a href="../chapter2/25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="../chapter2/26_Advanced_Operators.html">
            
                <a href="../chapter2/26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="01_About_the_Language_Reference.html">
            
                <a href="01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="02_Lexical_Structure.html">
            
                <a href="02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="03_Types.html">
            
                <a href="03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="04_Expressions.html">
            
                <a href="04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="05_Statements.html">
            
                <a href="05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="06_Declarations.html">
            
                <a href="06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="07_Attributes.html">
            
                <a href="07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="08_Patterns.html">
            
                <a href="08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.9" data-path="09_Generic_Parameters_and_Arguments.html">
            
                <a href="09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="10_Summary_of_the_Grammar.html">
            
                <a href="10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >泛型参数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6CDB;&#x578B;&#x53C2;&#x6570;&#xFF08;generic-parameters-and-arguments&#xFF09;">&#x6CDB;&#x578B;&#x53C2;&#x6570;&#xFF08;Generic Parameters and Arguments&#xFF09;</h1>
<p>&#x672C;&#x8282;&#x6D89;&#x53CA;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x3001;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x4EE5;&#x53CA;&#x6CDB;&#x578B;&#x6784;&#x9020;&#x5668;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5305;&#x62EC;&#x5F62;&#x53C2;&#x548C;&#x5B9E;&#x53C2;&#x3002;&#x58F0;&#x660E;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x3001;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x65F6;&#xFF0C;&#x987B;&#x6307;&#x5B9A;&#x76F8;&#x5E94;&#x7684;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x3002;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x5360;&#x4F4D;&#x7B26;&#xFF0C;&#x5F53;&#x5B9E;&#x4F8B;&#x5316;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x3001;&#x8C03;&#x7528;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x6CDB;&#x578B;&#x6784;&#x9020;&#x5668;&#x65F6;&#xFF0C;&#x5C31;&#x7528;&#x5177;&#x4F53;&#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x66FF;&#x4EE3;&#x4E4B;&#x3002;</p>
<p>&#x5173;&#x4E8E; Swift &#x8BED;&#x8A00;&#x7684;&#x6CDB;&#x578B;&#x6982;&#x8FF0;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/22_Generics.html">&#x6CDB;&#x578B;</a>&#x3002;</p>
<h2 id="generic-parameter">&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5; </h2>
<p><em>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;</em>&#x6307;&#x5B9A;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x6216;&#x51FD;&#x6570;&#x7684;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#xFF0C;&#x4EE5;&#x53CA;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x76F8;&#x5173;&#x7684;&#x7EA6;&#x675F;&#x548C;&#x8981;&#x6C42;&#x3002;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;&#x7528;&#x5C16;&#x62EC;&#x53F7;&#xFF08;<code>&lt;&gt;</code>&#xFF09;&#x5305;&#x4F4F;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>&lt;<code>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5217;&#x8868;</code>&gt;</p>
</blockquote>
<p>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5217;&#x8868;&#x4E2D;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x7528;&#x9017;&#x53F7;&#x5206;&#x5F00;&#xFF0C;&#x5176;&#x4E2D;&#x6BCF;&#x4E00;&#x4E2A;&#x91C7;&#x7528;&#x4EE5;&#x4E0B;&#x5F62;&#x5F0F;&#xFF1A;</p>
<blockquote>
<p><code>&#x7C7B;&#x578B;&#x5F62;&#x53C2;</code> : <code>&#x7EA6;&#x675F;</code></p>
</blockquote>
<p>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x7531;&#x4E24;&#x90E8;&#x5206;&#x7EC4;&#x6210;&#xFF1A;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x53CA;&#x5176;&#x540E;&#x7684;&#x53EF;&#x9009;&#x7EA6;&#x675F;&#x3002;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x53EA;&#x662F;&#x5360;&#x4F4D;&#x7B26;&#x7C7B;&#x578B;&#xFF08;&#x5982; <code>T</code>&#xFF0C;<code>U</code>&#xFF0C;<code>V</code>&#xFF0C;<code>Key</code>&#xFF0C;<code>Value</code> &#x7B49;&#xFF09;&#x7684;&#x540D;&#x5B57;&#x800C;&#x5DF2;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x3001;&#x51FD;&#x6570;&#x7684;&#x5176;&#x4F59;&#x90E8;&#x5206;&#x6216;&#x8005;&#x6784;&#x9020;&#x5668;&#x58F0;&#x660E;&#xFF0C;&#x5305;&#x62EC;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x7684;&#x7B7E;&#x540D;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#xFF08;&#x4EE5;&#x53CA;&#x5B83;&#x7684;&#x5173;&#x8054;&#x7C7B;&#x578B;&#xFF09;&#x3002;</p>
<p>&#x7EA6;&#x675F;&#x7528;&#x4E8E;&#x6307;&#x660E;&#x8BE5;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x7EE7;&#x627F;&#x81EA;&#x67D0;&#x4E2A;&#x7C7B;&#x6216;&#x8005;&#x7B26;&#x5408;&#x67D0;&#x4E2A;&#x534F;&#x8BAE;&#x6216;&#x534F;&#x8BAE;&#x7EC4;&#x5408;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x6CDB;&#x578B;&#x5F62;&#x53C2; <code>T: Comparable</code> &#x8868;&#x793A;&#x4EFB;&#x4F55;&#x7528;&#x4E8E;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#x5F62;&#x53C2; <code>T</code> &#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x5FC5;&#x987B;&#x6EE1;&#x8DB3; <code>Comparable</code> &#x534F;&#x8BAE;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">simpleMax</span>&lt;T: Comparable&gt;<span class="hljs-params">(<span class="hljs-number">_</span> x: T, <span class="hljs-number">_</span> y: T)</span></span> -&gt; <span class="hljs-type">T</span> {
    <span class="hljs-keyword">if</span> x &lt; y {
        <span class="hljs-keyword">return</span> y
    }
    <span class="hljs-keyword">return</span> x
}
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x56E0;&#x4E3A; <code>Int</code> &#x548C; <code>Double</code> &#x5747;&#x6EE1;&#x8DB3; <code>Comparable</code> &#x534F;&#x8BAE;&#xFF0C;&#x6240;&#x4EE5;&#x8BE5;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x8FD9;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x3002;&#x4E0E;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x76F8;&#x53CD;&#xFF0C;&#x8C03;&#x7528;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;&#x3002;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x7531;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x7684;&#x5B9E;&#x53C2;&#x63A8;&#x65AD;&#x800C;&#x51FA;&#x3002;</p>
<pre><code class="lang-swift">simpleMax(<span class="hljs-number">17</span>, <span class="hljs-number">42</span>) <span class="hljs-comment">// T &#x88AB;&#x63A8;&#x65AD;&#x4E3A; Int &#x7C7B;&#x578B;</span>
simpleMax(<span class="hljs-number">3.14159</span>, <span class="hljs-number">2.71828</span>) <span class="hljs-comment">// T &#x88AB;&#x63A8;&#x65AD;&#x4E3A; Double &#x7C7B;&#x578B;</span>
</code></pre>
<h3 id="where-clauses">Where &#x5B50;&#x53E5; </h3>
<p>&#x8981;&#x60F3;&#x5BF9;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x53CA;&#x5176;&#x5173;&#x8054;&#x7C7B;&#x578B;&#x6307;&#x5B9A;&#x989D;&#x5916;&#x8981;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x6216;&#x8005;&#x7C7B;&#x578B;&#x7684;&#x5927;&#x62EC;&#x53F7;&#x4E4B;&#x524D;&#x6DFB;&#x52A0; <code>where</code> &#x5B50;&#x53E5;&#x3002;<code>where</code> &#x5B50;&#x53E5;&#x7531;&#x5173;&#x952E;&#x5B57; <code>where</code> &#x53CA;&#x5176;&#x540E;&#x7684;&#x7528;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8981;&#x6C42;&#x7EC4;&#x6210;&#x3002;</p>
<blockquote>
<p><code>where</code> : <code>&#x7C7B;&#x578B;&#x8981;&#x6C42;</code></p>
</blockquote>
<p><code>where</code> &#x5B50;&#x53E5;&#x4E2D;&#x7684;&#x8981;&#x6C42;&#x7528;&#x4E8E;&#x6307;&#x660E;&#x8BE5;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x7EE7;&#x627F;&#x81EA;&#x67D0;&#x4E2A;&#x7C7B;&#x6216;&#x7B26;&#x5408;&#x67D0;&#x4E2A;&#x534F;&#x8BAE;&#x6216;&#x534F;&#x8BAE;&#x7EC4;&#x5408;&#x3002;&#x5C3D;&#x7BA1; <code>where</code> &#x5B50;&#x53E5;&#x63D0;&#x4F9B;&#x4E86;&#x8BED;&#x6CD5;&#x7CD6;&#x4F7F;&#x5176;&#x6709;&#x52A9;&#x4E8E;&#x8868;&#x8FBE;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x4E0A;&#x7684;&#x7B80;&#x5355;&#x7EA6;&#x675F;&#xFF08;&#x5982; <code>&lt;T: Comparable&gt;</code> &#x7B49;&#x540C;&#x4E8E; <code>&lt;T&gt; where T: Comparable</code>&#xFF0C;&#x7B49;&#x7B49;&#xFF09;&#xFF0C;&#x4F46;&#x662F;&#x4F9D;&#x7136;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5BF9;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x53CA;&#x5176;&#x5173;&#x8054;&#x7C7B;&#x578B;&#x63D0;&#x4F9B;&#x66F4;&#x590D;&#x6742;&#x7684;&#x7EA6;&#x675F;&#xFF0C;&#x4F8B;&#x5982;&#x4F60;&#x53EF;&#x4EE5;&#x5F3A;&#x5236;&#x5F62;&#x53C2;&#x7684;&#x5173;&#x8054;&#x7C7B;&#x578B;&#x9075;&#x5B88;&#x534F;&#x8BAE;&#xFF0C;&#x5982;&#xFF0C;<code>&lt;S: Sequence&gt; where S.Iterator.Element: Equatable</code> &#x8868;&#x793A;&#x6CDB;&#x578B;&#x7C7B;&#x578B; <code>S</code> &#x9075;&#x5B88; <code>Sequence</code> &#x534F;&#x8BAE;&#x5E76;&#x4E14;&#x5173;&#x8054;&#x7C7B;&#x578B; <code>S.Iterator.Element</code> &#x9075;&#x5B88; <code>Equatable</code> &#x534F;&#x8BAE;&#xFF0C;&#x8FD9;&#x4E2A;&#x7EA6;&#x675F;&#x786E;&#x4FDD;&#x961F;&#x5217;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x90FD;&#x662F;&#x7B26;&#x5408; <code>Equatable</code> &#x534F;&#x8BAE;&#x7684;&#x3002;</p>
<blockquote>
</blockquote>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x64CD;&#x4F5C;&#x7B26; <code>==</code> &#x6765;&#x6307;&#x5B9A;&#x4E24;&#x4E2A;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x76F8;&#x540C;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5; <code>&lt;S1: Sequence, S2: Sequence&gt; where S1.Iterator.Element == S2.Iterator.Element</code> &#x8868;&#x793A; <code>S1</code> &#x548C; <code>S2</code> &#x5FC5;&#x987B;&#x90FD;&#x7B26;&#x5408; <code>SequenceType</code> &#x534F;&#x8BAE;&#xFF0C;&#x800C;&#x4E14;&#x4E24;&#x4E2A;&#x5E8F;&#x5217;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x76F8;&#x540C;&#x3002;</p>
<blockquote>
</blockquote>
<p>&#x5F53;&#x7136;&#xFF0C;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x5FC5;&#x987B;&#x6EE1;&#x8DB3;&#x6240;&#x6709;&#x7684;&#x7EA6;&#x675F;&#x548C;&#x8981;&#x6C42;&#x3002;</p>
<p>&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x53EF;&#x4EE5;&#x91CD;&#x8F7D;&#xFF0C;&#x4F46;&#x5728;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x5FC5;&#x987B;&#x6709;&#x4E0D;&#x540C;&#x7684;&#x7EA6;&#x675F;&#x6216;&#x8981;&#x6C42;&#xFF0C;&#x6291;&#x6216;&#x4E8C;&#x8005;&#x7686;&#x4E0D;&#x540C;&#x3002;&#x5F53;&#x8C03;&#x7528;&#x91CD;&#x8F7D;&#x7684;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x65F6;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4F1A;&#x6839;&#x636E;&#x8FD9;&#x4E9B;&#x7EA6;&#x675F;&#x6765;&#x51B3;&#x5B9A;&#x8C03;&#x7528;&#x54EA;&#x4E2A;&#x91CD;&#x8F7D;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x6CDB;&#x578B; where &#x4ECE;&#x53E5;&#x7684;&#x4FE1;&#x606F;&#x548C;&#x5173;&#x4E8E;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x4E00;&#x770B; <a href="../chapter2/22_Generics.html#where_clauses">&#x6CDB;&#x578B; where &#x5B50;&#x53E5;</a>&#x3002;</p>
<blockquote>
<p>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="generic-parameter-clause">generic-parameter-clause </h4>
<blockquote>
<p><em>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;</em> &#x2192; <strong>&lt;</strong> <a href="#generic-parameter-list"><em>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5217;&#x8868;</em></a> <a href="#requirement-clause"><em>&#x7EA6;&#x675F;&#x5B50;&#x53E5;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>&gt;</strong></p>
</blockquote>
<h4 id="generic-parameter-list">generic-parameter-list </h4>
<blockquote>
<p><em>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5217;&#x8868;</em> &#x2192; <a href="#generic-parameter"><em>&#x6CDB;&#x5F62;&#x5F62;&#x53C2;</em></a> | <a href="#generic-parameter"><em>&#x6CDB;&#x5F62;&#x5F62;&#x53C2;</em></a> <strong>,</strong> <a href="#generic-parameter-list"><em>&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="generic-parameter">generic-parameter </h4>
<blockquote>
<p><em>&#x6CDB;&#x5F62;&#x5F62;&#x53C2;</em> &#x2192; <a href="03_Types.html#type-name"><em>&#x7C7B;&#x578B;&#x540D;&#x79F0;</em></a></p>
<p><em>&#x6CDB;&#x5F62;&#x5F62;&#x53C2;</em> &#x2192; <a href="03_Types.html#type-name"><em>&#x7C7B;&#x578B;&#x540D;&#x79F0;</em></a>    <strong>:</strong> <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x7B26;</em></a></p>
<p><em>&#x6CDB;&#x5F62;&#x5F62;&#x53C2;</em> &#x2192; <a href="03_Types.html#type-name"><em>&#x7C7B;&#x578B;&#x540D;&#x79F0;</em></a>    <strong>:</strong> <a href="03_Types.html#protocol-composition-type"><em>&#x534F;&#x8BAE;&#x5408;&#x6210;&#x7C7B;&#x578B;</em></a></p>
<h4 id="requirement-clause">requirement-clause </h4>
<p><em>&#x7EA6;&#x675F;&#x5B50;&#x53E5;</em> &#x2192; <strong>where</strong> <a href="#requirement-list"><em>&#x7EA6;&#x675F;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="requirement-list">requirement-list </h4>
<blockquote>
<p><em>&#x7EA6;&#x675F;&#x5217;&#x8868;</em> &#x2192; <a href="#requirement"><em>&#x7EA6;&#x675F;</em></a> | <a href="#requirement"><em>&#x7EA6;&#x675F;</em></a> <strong>,</strong> <a href="#requirement-list"><em>&#x7EA6;&#x675F;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="requirement">requirement </h4>
<blockquote>
<p><em>&#x7EA6;&#x675F;</em> &#x2192; <a href="#conformance-requirement"><em>&#x4E00;&#x81F4;&#x6027;&#x7EA6;&#x675F;</em></a> | <a href="#same-type-requirement"><em>&#x540C;&#x7C7B;&#x578B;&#x7EA6;&#x675F;</em></a></p>
<h4 id="conformance-requirement">conformance-requirement </h4>
<p><em>&#x4E00;&#x81F4;&#x6027;&#x7EA6;&#x675F;</em> &#x2192; <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x7B26;</em></a> <strong>:</strong> <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x7B26;</em></a></p>
<p><em>&#x4E00;&#x81F4;&#x6027;&#x7EA6;&#x675F;</em> &#x2192; <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x7B26;</em></a> <strong>:</strong> <a href="03_Types.html#protocol-composition-type"><em>&#x534F;&#x8BAE;&#x5408;&#x6210;&#x7C7B;&#x578B;</em></a></p>
</blockquote>
<h4 id="same-type-requirement">same-type-requirement </h4>
<blockquote>
<p><em>&#x540C;&#x7C7B;&#x578B;&#x7EA6;&#x675F;</em> &#x2192; <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;&#x7B26;</em></a> <strong>==</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
</blockquote>
<h2 id="generic-argument">&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5; </h2>
<p><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;</em>&#x6307;&#x5B9A;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x3002;&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;&#x7528;&#x5C16;&#x62EC;&#x53F7;&#xFF08;<code>&lt;&gt;</code>&#xFF09;&#x5305;&#x4F4F;&#xFF0C;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>&lt;<code>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5217;&#x8868;</code>&gt;</p>
</blockquote>
<p>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5217;&#x8868;&#x4E2D;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x7528;&#x9017;&#x53F7;&#x5206;&#x5F00;&#x3002;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x662F;&#x5B9E;&#x9645;&#x5177;&#x4F53;&#x7C7B;&#x578B;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x7528;&#x6765;&#x66FF;&#x4EE3;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x7684;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;&#x4E2D;&#x7684;&#x76F8;&#x5E94;&#x7684;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#x3002;&#x4ECE;&#x800C;&#x5F97;&#x5230;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x7684;&#x4E00;&#x4E2A;&#x7279;&#x5316;&#x7248;&#x672C;&#x3002;&#x4F8B;&#x5982;&#xFF0C;Swift &#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684;&#x6CDB;&#x578B;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x7684;&#x7684;&#x7B80;&#x5316;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Dictionary</span>&lt;<span class="hljs-title">Key</span>: <span class="hljs-title">Hashable</span>, <span class="hljs-title">Value</span>&gt;: <span class="hljs-title">CollectionType</span>, <span class="hljs-title">DictionaryLiteralConvertible</span> </span>{
    <span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>&#x6CDB;&#x578B; <code>Dictionary</code> &#x7C7B;&#x578B;&#x7684;&#x7279;&#x5316;&#x7248;&#x672C;&#xFF0C;<code>Dictionary&lt;String, Int&gt;</code> &#x5C31;&#x662F;&#x7528;&#x5177;&#x4F53;&#x7684; <code>String</code> &#x548C; <code>Int</code> &#x7C7B;&#x578B;&#x66FF;&#x4EE3;&#x6CDB;&#x578B;&#x7C7B;&#x578B; <code>Key: Hashable</code> &#x548C; <code>Value</code> &#x4EA7;&#x751F;&#x7684;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x5FC5;&#x987B;&#x6EE1;&#x8DB3;&#x5B83;&#x6240;&#x66FF;&#x4EE3;&#x7684;&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x7684;&#x6240;&#x6709;&#x7EA6;&#x675F;&#xFF0C;&#x5305;&#x62EC;&#x4EFB;&#x4F55; <code>where</code> &#x5B50;&#x53E5;&#x6240;&#x6307;&#x5B9A;&#x7684;&#x989D;&#x5916;&#x7684;&#x5173;&#x8054;&#x7C7B;&#x578B;&#x8981;&#x6C42;&#x3002;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x7C7B;&#x578B;&#x5F62;&#x53C2; <code>Key</code> &#x7684;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x7B26;&#x5408; <code>Hashable</code> &#x534F;&#x8BAE;&#xFF0C;&#x56E0;&#x6B64; <code>String</code> &#x4E5F;&#x5FC5;&#x987B;&#x6EE1;&#x8DB3; <code>Hashable</code> &#x534F;&#x8BAE;&#x3002;</p>
<p>&#x53EF;&#x4EE5;&#x7528;&#x672C;&#x8EAB;&#x5C31;&#x662F;&#x6CDB;&#x578B;&#x7C7B;&#x578B;&#x7684;&#x7279;&#x5316;&#x7248;&#x672C;&#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#x5F62;&#x53C2;&#xFF08;&#x5047;&#x8BBE;&#x5DF2;&#x6EE1;&#x8DB3;&#x5408;&#x9002;&#x7684;&#x7EA6;&#x675F;&#x548C;&#x5173;&#x8054;&#x7C7B;&#x578B;&#x8981;&#x6C42;&#xFF09;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E3A;&#x4E86;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x662F;&#x6574;&#x578B;&#x6570;&#x7EC4;&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x7279;&#x5316;&#x7248;&#x672C; <code>Array&lt;Int&gt;</code> &#x66FF;&#x4EE3;&#x6CDB;&#x578B;&#x7C7B;&#x578B; <code>Array&lt;T&gt;</code> &#x7684;&#x7C7B;&#x578B;&#x5F62;&#x53C2; <code>T</code> &#x6765;&#x5B9E;&#x73B0;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> arrayOfArrays: <span class="hljs-type">Array</span>&lt;<span class="hljs-type">Array</span>&lt;<span class="hljs-type">Int</span>&gt;&gt; = [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]]
</code></pre>
<p>&#x5982; <a href="#generic_parameter">&#x6CDB;&#x578B;&#x5F62;&#x53C2;&#x5B50;&#x53E5;</a> &#x6240;&#x8FF0;&#xFF0C;&#x4E0D;&#x80FD;&#x7528;&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;&#x6765;&#x6307;&#x5B9A;&#x6CDB;&#x578B;&#x51FD;&#x6570;&#x6216;&#x6784;&#x9020;&#x5668;&#x7684;&#x7C7B;&#x578B;&#x5B9E;&#x53C2;&#x3002;</p>
<blockquote>
<p>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="generic-argument-clause">generic-argument-clause </h4>
<blockquote>
<p><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5B50;&#x53E5;</em> &#x2192; <strong>&lt;</strong> <a href="#generic-argument-list"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5217;&#x8868;</em></a> <strong>&gt;</strong></p>
</blockquote>
<h4 id="generic-argument-list">generic-argument-list </h4>
<blockquote>
<p><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5217;&#x8868;</em> &#x2192; <a href="#generic-argument"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;</em></a> | <a href="#generic-argument"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;</em></a> <strong>,</strong> <a href="#generic-argument-list"><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="generic-argument">generic-argument </h4>
<blockquote>
<p><em>&#x6CDB;&#x578B;&#x5B9E;&#x53C2;</em> &#x2192; <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="08_Patterns.html" class="navigation navigation-prev " aria-label="Previous page: 模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="10_Summary_of_the_Grammar.html" class="navigation navigation-next " aria-label="Next page: 语法总结">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"泛型参数","level":"1.4.9","depth":2,"next":{"title":"语法总结","level":"1.4.10","depth":2,"path":"chapter3/10_Summary_of_the_Grammar.md","ref":"chapter3/10_Summary_of_the_Grammar.md","articles":[]},"previous":{"title":"模式","level":"1.4.8","depth":2,"path":"chapter3/08_Patterns.md","ref":"chapter3/08_Patterns.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter3/09_Generic_Parameters_and_Arguments.md","mtime":"2019-07-05T01:01:08.290Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

